<%--
    Document   : driver
    Created on : 2013-4-15, 12:26:07
    Author     : piaohao
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
    <head>
        <base href="<%= basePath%>"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@include file="../main/common.jsp" %>
        <title>驾驶员档案</title>
        <script type="text/javascript">
            Ext.onReady(function() {
                var driverForm = Ext.create("Ext.form.Panel", {
                    url: "driver/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 2
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "姓名", name: "driver.name", allowBlank: false},
                        {
                            id: "orgCombo",
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "所属机构",
                            name: "driver.org_id",
                            emptyText: "--请选择--",
                            displayField: 'name',
                            valueField: 'id',
                            editable: false,
                            allowBlank: false,
                            store: {
                                fields: ['name', 'id'],
                                proxy: {
                                    type: 'ajax',
                                    url: 'org/getAll',
                                    reader: {
                                        type: 'json'
                                    }
                                }
                            }
                        },
                        {
                            xtype: "combo",
                            fieldLabel: '性别',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "男", value: "男"},
                                    {text: "女", value: "女"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "driver.sex",
                            value: "男"
                        },
                        {fieldLabel: "年龄", name: "driver.age", regex: Validator.number, regexText: "年龄格式不对"},
                        {fieldLabel: "出生日期", name: "birthday", xtype: "datefield", editable: false, format: "Y-m-d"},
                        {fieldLabel: "身份证", name: "driver.id_card", regex: Validator.idCard, regexText: "身份证格式不对", allowBlank: false},
                        {fieldLabel: "电话", name: "driver.cellphone", regex: Validator.number, regexText: "电话格式不对"},
                        {fieldLabel: "邮件", name: "driver.email", regex: Validator.email, regexText: "邮件格式不对"},
                        {
                            xtype: "combo",
                            fieldLabel: '文化程度',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "小学", value: "小学"},
                                    {text: "初中", value: "初中"},
                                    {text: "中专", value: "中专"},
                                    {text: "高中", value: "高中"},
                                    {text: "大专", value: "大专"},
                                    {text: "本科", value: "本科"},
                                    {text: "硕士", value: "硕士"},
                                    {text: "博士", value: "博士"},
                                    {text: "博士后", value: "博士后"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "driver.education",
                            emptyText: "--请选择--",
                            value: ""
                        },
                        {fieldLabel: "家庭住址", name: "driver.address"},
                        {name: "driver.id", xtype: "hiddenfield"}
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                driverStore.load();
                                                driverWindow.close();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                driverWindow.close();
                            }
                        }]
                });
                var driverWindow = Ext.create("Ext.window.Window", {
                    title: "驾驶员档案",
                    width: 550,
                    height: 300,
                    maximizable: true,
                    closeAction: "hide",
                    layout: "fit",
                    modal: true,
                    items: [driverForm]
                });
                var driverStore = Ext.create('Ext.data.Store', {
                    fields: ["id", "name", "age", "birthday", "sex", "id_card", "cellphone", "email", "education", "address", "org_id", "orgName"],
                    pageSize: 15,
                    proxy: {
                        type: 'ajax',
                        url: 'driver/list',
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                    },
                    autoLoad: true
                });
                var driverGrid = Ext.create("Ext.grid.Panel", {
                    title: "驾驶员档案",
                    columnLines: true,
                    frame: true,
                    selType: 'checkboxmodel', // 设置选择模式 
                    multiSelect: true, // 允许多选
                    forceFit: true,
                    columns: [
                        {header: '主键', dataIndex: 'id', hidden: true},
                        {header: '机构ID', dataIndex: 'org_id', hidden: true},
                        {header: '姓名', dataIndex: 'name', width: 120},
                        {header: '年龄', dataIndex: 'age', width: 100},
                        {header: '出生日期', dataIndex: 'birthday', width: 100},
                        {header: '性别', dataIndex: 'sex', width: 100},
                        {header: '身份证', dataIndex: 'id_card', width: 100},
                        {header: '电话', dataIndex: 'cellphone', width: 100},
                        {header: '邮件', dataIndex: 'email', width: 100},
                        {header: '文化程度', dataIndex: 'education', width: 100},
                        {header: '家庭住址', dataIndex: 'address', width: 200},
                        {header: '所属机构', dataIndex: 'orgName', width: 200}
                    ],
                    store: driverStore,
                    tbar: [
                        {xtype: "button", text: "查看", iconCls: "search",
                            handler: function() {
                                var sel = driverGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0].data;
                                var detailWindow = Ext.create("Ext.window.Window", {
                                    title: "驾驶员档案",
                                    width: 800,
                                    height: 400,
                                    maximizable: true,
                                    modal: true,
                                    closeAction: "hide",
                                    layout: "fit",
                                    html: "<iframe id='detailFrame' src='driver/showDetail/" + record.id + "' width='100%' height='100%'></iframe>",
                                    buttons: [
                                        {text: "打印", iconCls: "print",
                                            handler: function() {
                                                Ext.get("detailFrame").dom.contentWindow.print();
                                            }},
                                        {text: "关闭", iconCls: "close",
                                            handler: function() {
                                                detailWindow.close();
                                            }}
                                    ]
                                });
                                detailWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "登记", iconCls: "add",
                            handler: function() {
                                driverForm.getForm().reset();
                                driverForm.getForm().url = "driver/add";
                                driverWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "变更", iconCls: "edit",
                            handler: function() {
                                var sel = driverGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                driverForm.getForm().reset();
                                var record = sel[0];
                                driverForm.getForm().url = "driver/update";
                                driverWindow.show();
                                driverForm.getForm().setValues({
                                    "driver.id": record.get("id"),
                                    "driver.name": record.get("name"),
                                    "driver.age": record.get("age"),
                                    "birthday": record.get("birthday").substring(0, 10),
                                    "driver.org_id": record.get("org_id"),
                                    "driver.sex": record.get("sex"),
                                    "driver.id_card": record.get("id_card"),
                                    "driver.cellphone": record.get("cellphone"),
                                    "driver.email": record.get("email"),
                                    "driver.education": record.get("education"),
                                    "driver.address": record.get("address")
                                });
                                Ext.getCmp("orgCombo").getStore().load();
                            }
                        },
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "删除", iconCls: "remove",
                            handler: function() {
                                Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                    if (id === "yes") {
                                        var mask = new Ext.LoadMask(driverGrid, {msg: "正在删除..."});
                                        mask.show();
                                        var sel = driverGrid.getSelectionModel().getSelection();
                                        var ids = [];
                                        for (i = 0; i < sel.length; i++) {
                                            ids.push(sel[i].data.id);
                                        }
                                        Ext.Ajax.request({
                                            url: "driver/delete",
                                            params: {
                                                ids: ids
                                            },
                                            success: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    driverGrid.getStore().remove(sel);
                                                    mask.hide();
                                                });
                                            },
                                            failure: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    mask.hide();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        }, "-", {
                            text: "重新加载数据",
                            iconCls: "refresh",
                            handler: function() {
                                driverStore.setProxy({
                                    type: "ajax",
                                    url: "driver/list",
//                                    extraParams: {
//                                        name: record.name,
//                                        startDate: record.startDate,
//                                        endDate: record.endDate,
//                                        startAge: record.startAge,
//                                        endAge: record.endAge,
//                                        orgId: record.org_id
//                                    },
                                    reader: {
                                        type: "json",
                                        root: "data"
                                    }
                                });
                                driverStore.load();
                                driverStore.load({url: "driver/list"});
                            }
                        }, "-", {
                            text: "工具",
                            icon: "resource/images/ico/function.gif",
                            menu: {
                                xtype: "menu",
                                items: [{
                                        text: "导出当前页数据",
                                        iconCls: "xls",
                                        handler: function() {
                                            window.open("driver/exportAll?start=" + driverStore.currentPage * 15, "_blank");
                                        }
                                    }, {
                                        text: "导出所有数据",
                                        iconCls: "xls",
                                        handler: function() {
                                            window.open("driver/exportAll", "_blank");
                                        }
                                    }]
                            }
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: driverStore,
                        displayInfo: true
                    })
                });
                var searchForm = Ext.create("Ext.form.Panel", {
                    url: "driver/search",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 1
                    },
                    defaults: {
                        labelAlign: "right",
                        labelWidth: 80
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "姓名", name: "name"},
                        {fieldLabel: "出生日期", name: "startDate", xtype: "datefield", editable: false, format: "Y-m-d"},
                        {fieldLabel: "&nbsp;", name: "endDate", xtype: "datefield", editable: false, format: "Y-m-d"},
                        {fieldLabel: "年龄", name: "startAge", xtype: "numberfield"},
                        {fieldLabel: "&nbsp;", name: "endAge", xtype: "numberfield"},
                        {
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "所属机构",
                            name: "org_id",
                            emptyText: "--请选择--",
                            displayField: 'name',
                            valueField: 'id',
                            editable: false,
                            allowBlank: false,
                            store: {
                                fields: ['name', 'id'],
                                proxy: {
                                    type: 'ajax',
                                    url: 'org/getAll',
                                    reader: {
                                        type: 'json'
                                    }
                                }
                            }
                        }
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            iconCls: "save",
                            handler: function() {
                                var record = this.up('form').getForm().getValues();
                                driverStore.setProxy({
                                    type: "ajax",
                                    url: "driver/search",
                                    extraParams: {
                                        name: record.name,
                                        startDate: record.startDate,
                                        endDate: record.endDate,
                                        startAge: record.startAge,
                                        endAge: record.endAge,
                                        orgId: record.org_id
                                    },
                                    reader: {
                                        type: "json",
                                        root: "data"
                                    }
                                });
                                driverStore.load();
                            }
                        }]
                });
                var viewport = Ext.create("Ext.Viewport", {
                    layout: {type: "border"},
                    items: [
                        {region: "center", layout: "fit", items: [driverGrid]},
                        {region: "east", layout: "fit", width: 300, collapsed: true, collapsible: true, title: "高级查询", items: [searchForm]}
                    ]
                });
            });
        </script>

    </head>
    <body>
    </body>
</html>
